﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using DAL;

namespace PeisongWeb.admin
{
    public partial class companyScoreList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("FD", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        // 企业类别
        void drpBind()
        {
            DataTable dt = logic.sysCode.companyTypeList();
            ddl_Type.DataSource = dt;
            ddl_Type.DataBind();
            ddl_Type.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));

            ListItem item = ddl_Type.Items.FindByText("供应商");
            if (item!=null)
            {
                item.Selected = true;
            }
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("isCheck=1");
            if (txt_companyName.Text.Trim() != "")
                where.AppendFormat("and company like '%{0}%'", txt_companyName.Text.Trim());
            if (ddl_Type.SelectedValue != "")
                where.AppendFormat("and type = '{0}'", ddl_Type.SelectedValue.Trim());
            if (txt_mobile.Text.Trim() != "")
                where.AppendFormat("and mobile = '{0}'", txt_mobile.Text.Trim());
            DataSet ds = logic.company.select(where.ToString(), "companyId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void link_CompanyCheck_Command(object sender,CommandEventArgs e)
        {
            hdCurrentCompanyId.Value = e.CommandArgument.ToString();
            windows1.Show();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            CompanyScoreEntity companyScore = new CompanyScoreEntity();
            companyScore.AdminId = logic.sysAdmin.AdminID;
            companyScore.Reason = txtReason.Text;
            int companyId = 0;
            int.TryParse(hdCurrentCompanyId.Value,out companyId);
            companyScore.CompanyId = companyId;
            int score = 0;
            int.TryParse(txtScore.Text, out score);
            companyScore.Score = (ddlOperate.SelectedValue == "0" ? -score : score);
            companyScore.Type = ddl_Type.SelectedValue;

            int returnValue=logic.companyScore.insert(companyScore);
            if (returnValue != 0)
            {
                Library.Script.ClientMsg("保存成功！");
                txtReason.Text = "";
                txtScore.Text = "";
                ddlOperate.SelectedValue = "0";
                rptBind();
            }
            else
            {
                Library.Script.ClientMsg("操作失败！原因可能是：企业得分等于零或者大于最高得分("+logic.webset.list().CompanyScore+")!");
                return;
            }
        }
    }
}
